package com.shms.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SQLQuery;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.shms.bean.GroupOfMessage;
import com.shms.bean.IndexParam;
import com.shms.dao.ShMessageDao;
import com.shms.entity.ShMessage;
import com.shms.until.PageUntil;

/**
 * 文件名：ShMessageDaoImpl.java
 * 编写者：杨勇
 * 创建时间：2013-4-12
 * 简要描述：
 * 
 */
public class ShMessageDaoImpl extends HibernateDaoSupport implements ShMessageDao {
	@SuppressWarnings({ "finally", "unchecked" })
	@Override
	public List<ShMessage> queryMessageByExample(IndexParam indexParam) {
		@SuppressWarnings("rawtypes")
		List<ShMessage> list = new ArrayList();
		try {
			StringBuffer strBuf = new StringBuffer();
			strBuf.append(" SELECT * FROM ");
			strBuf.append(" (SELECT * FROM SH_MESSAGE ORDER BY MTIME DESC) ");
			strBuf.append(" WHERE ROWNUM <= :param ");
			strBuf.append(" ORDER BY ROWNUM ");
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(strBuf.toString());
			sqlQuery.setProperties(indexParam);
			list = sqlQuery.addEntity(ShMessage.class).list();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return list;
		}
	}
	
	@SuppressWarnings("finally")
	public GroupOfMessage queryMessage(GroupOfMessage groupOfMessage) {
		GroupOfMessage gOfMessageRtn = new GroupOfMessage();
		try {
			//查询数据总条数的SQL语句
			String sql = "SELECT COUNT(*) FROM SH_MESSAGE";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			//获取总条数
			int allRow = Integer.parseInt(String.valueOf(sqlQuery.uniqueResult()));
			//获取总页数
			int totalPage = PageUntil.countTotalPage(groupOfMessage.getPageSize(), allRow);
			//获取要查询显示的数据的页数
			int currentPage = PageUntil.countCurrentPage(groupOfMessage.getCurrentPage(), totalPage);
			//获取要查询显示的数据的第一条的数字
			int currentRow = PageUntil.countCurrentRow(groupOfMessage.getPageSize(), currentPage);
			
			sql = "SELECT * FROM SH_MESSAGE ORDER BY MTIME DESC";
			sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			//设置查询的第一条数据位置
			sqlQuery.setFirstResult(currentRow);
			//设置查询最大条数
			sqlQuery.setMaxResults(groupOfMessage.getPageSize());
			@SuppressWarnings("unchecked")
			List<ShMessage> messageListRtn = sqlQuery.addEntity(ShMessage.class).list();
			gOfMessageRtn = new GroupOfMessage(currentPage, totalPage, allRow, messageListRtn); 
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return gOfMessageRtn;
		}
	}
	
	@SuppressWarnings("finally")
	public ShMessage queryById(ShMessage shMessage) {
		ShMessage shMessagesRtn = new ShMessage();
		try {
			String sql = "SELECT * FROM SH_MESSAGE WHERE MID = :mid";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			sqlQuery.setProperties(shMessage);
			@SuppressWarnings("unchecked")
			List<ShMessage> listMessage = sqlQuery.addEntity(ShMessage.class).list();
			if (listMessage.size() == 1) {
				shMessagesRtn = listMessage.get(0);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return shMessagesRtn;
		}
	}
	
	@SuppressWarnings("finally")
	public boolean saveMessage(ShMessage shMessage) {
		boolean rtn = true;
		try {
			this.getSessionFactory().getCurrentSession().save(shMessage);
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}
}
